package com.mofang.ticketing.common.security.util;

import com.mofang.ticketing.common.security.bo.UserDetailBO;
import com.mofang.ticketing.common.security.bo.UserInfoBO;
import org.springframework.lang.Nullable;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;

/**
 * 获取认证用户信息
 *
 * @author LGC
 */
public class SecurityUtil {

    /**
     * 获取所属组织
     */
    public static Integer getBelong() {
        UserInfoBO userInfo = getUserInfo();
        return userInfo != null ? userInfo.getBelong() : null;
    }

    /**
     * 获取账号
     */
    public static String getAccount() {
        UserInfoBO userInfo = getUserInfo();
        return userInfo != null ? userInfo.getAccount() : null;
    }

    /**
     * 获取账号类型
     */
    public static Integer getAccountType() {
        UserInfoBO userInfo = getUserInfo();
        return userInfo != null ? userInfo.getAccountType() : null;
    }

    /**
     * 获取用户ID
     */
    public static Long getUserId() {
        UserInfoBO userInfo = getUserInfo();
        return userInfo != null ? userInfo.getId() : null;
    }

    /**
     * 获取角色
     */
    public static Set<String> getRoles() {
        UserDetailBO loginUser = getLoginUser();
        return loginUser != null ? loginUser.getRoles() : null;
    }

    /**
     * 获取权限
     */
    public static Set<String> getPerms() {
        UserDetailBO loginUser = getLoginUser();
        return loginUser != null ? loginUser.getPerms() : null;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static UserInfoBO getUserInfo() {
        UserDetailBO loginUser = getLoginUser();
        return loginUser != null ? loginUser.getUserInfoBO() : null;
    }

    @Nullable
    public static UserDetailBO getLoginUser() {
        Authentication authentication = getAuthentication();
        if (authentication == null) {
            return null;
        }
        return authentication.getPrincipal() instanceof UserDetailBO ? (UserDetailBO) authentication.getPrincipal() : null;
    }

    public static Authentication getAuthentication() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            return null;
        }
        return context.getAuthentication();
    }

    public static void setLoginUser(UserDetails userDetails) {
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }

}
